import {
  Body,
  Controller,
  HttpException,
  HttpStatus,
  Post,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { User } from 'libs/db/src/model/user.model';
import { Result } from 'src/common/Result';
import { UsersService } from './users.service';

@ApiTags('用户')
@Controller('user')
export class UsersController {
  constructor(private readonly userService: UsersService) {}

  @Post('/')
  async create(@Body() createUserDto: User): Promise<Result> {
    try {
      await this.userService.create(createUserDto);
      return {
        message: '创建成功',
      };
    } catch (error) {
      throw new HttpException(
        {
          status: HttpStatus.NOT_ACCEPTABLE,
          error: '用户名重复，请更换用户名',
        },
        HttpStatus.NOT_ACCEPTABLE,
      );
    }
  }
}
